<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="./js/jquery.min.js"></script><script src="./js/common.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="./js/html5.min.js"></script>
    <script src="./js/respond.min.js"></script>
    <![endif]-->

    <script>
        var baseUrl = parent.baseUrl;
        var env = $.getUrlParam("env");
        var gateway = $.getUrlParam("gateway");

        function getExtalConfigDiv(configData) {
            var id = 'l_extral_' + configData.id;
            var lable = configData.name;
            var name = configData.name;
            var comment = configData.comment;
            var value = configData.value;


            var temp = '<div class="layui-form-item">\n' +
                '                <label for="' + id + '" class="layui-form-label" style="width: 250px;">\n' +
                '                    <span class="x-red">*</span>' + lable + ':\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="text" value="' + value + '" id="' + id + '" name="' + name + '" required="" lay-verify="required"\n' +
                '                           autocomplete="off" class="layui-input id-input">\n' +
                '                </div>\n' +


                '                <label for="' + id + '_comment" class="layui-form-label">\n' +
                '                    <span class="x-red">*</span>备注:\n' +
                '                </label>\n' +

                '                <div class="layui-input-inline" >\n' +
                '                    <input type="text" value="' + comment + '" id="' + id + '_comment" name="' + name + '" required="" lay-verify="required"\n' +
                '                           autocomplete="off" class="layui-input comment-input">\n' +
                '                </div>\n' +

                '                <div class="layui-input-inline">\n' +
                '                    <button type="button" class="layui-btn layui-btn-normal" onclick="editConfig(this)">修改</button>\n' +
                // '<span class="layui-word-aux">\n' +
                // comment +
                // '            </span>' +
                '                </div>\n' +
                '            </div>';

            return temp;
        }

        function editConfig(obj, delVal, notShowSuccTip) {

            var idInput = $(obj).closest('.layui-form-item').find('.id-input');
            var name = idInput.attr("name");
            var value = idInput.val();
            if (!value) {
                value = '';
            }

            if (delVal != undefined) {
                value = delVal + "";
            }

            value = $.trim(value);

            var comment = $(obj).closest('.layui-form-item').find('.comment-input').val();
            if (!comment) {
                comment = '';
            }

            comment = $.trim(comment);

            var gatewayConfUpdateReq = {
                request: {
                    applicationName: gateway, name: name, value: value, comment: comment
                }
            };

            var editSuc = false;
            $.ajax({
                url: baseUrl + "/servcieGateway/config/update?env=" + env + "&gateway=" + gateway,
                data: JSON.stringify(gatewayConfUpdateReq),
                contentType: 'application/json;charset=utf-8',
                dataType: "JSON",
                async: false,
                type: "POST",
                success: function (respData) {
                    if (respData.respCode === '0000') {
                        if (!notShowSuccTip) {
                            var index = layer.alert("修改成功", {icon: 6}, function () {
                                layer.close(index);
                                window.location.reload();
                            });
                        }
                        editSuc = true;
                    } else {
                        layer.msg(respData.respDesc, {
                            icon: 2,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            //do something
                        });
                    }
                }
            });

            return editSuc;
        }


        $(function () {

            var gatewayConfReq = {
                request: {
                    applicationName: gateway, zuulFilterId: "__global_config_relation__"
                }
            };

            $.ajax({
                url: baseUrl + "/servcieGateway/config/list?env=" + env + "&gateway=" + gateway,
                data: JSON.stringify(gatewayConfReq),
                contentType: 'application/json;charset=utf-8',
                dataType: "JSON",
                async: false,
                type: "POST",
                success: function (respData) {
                    if (respData.respCode === '0000') {
                        try {
                            respData.data.items.forEach(function (_val, _inx) {
                                if (_val.name === 'zuul.use.canary.filters') {
                                    if (_val.value == 'true') {
                                        $("#l_canary_status").attr("checked", "");
                                    }
                                } else if (_val.name === 'application.log.detail') {
                                    if (_val.value == 'true') {
                                        $('#l_application_log_detail').attr("checked", "");
                                    }
                                } else {
                                    var extalConfigDiv = getExtalConfigDiv(_val);
                                    $("#extralConfigDiv").append(extalConfigDiv);
                                }

                            });
                        } catch (e) {

                        }
                    } else {
                        parent.layer.msg(respData.respDesc, {
                            icon: 2,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            //do something
                        });
                    }
                }
            });

        });
    </script>
</head>

<body>
<div class="x-body layui-anim layui-anim-up">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="l_canary_status" class="layui-form-label"
                   style="margin-top: -7px;width: 250px;">灰度过滤器启用开关：</label>
            <div class="layui-input-block">
                <input id="l_canary_status" type="checkbox" name="zuul.use.canary.filters" lay-skin="switch"
                       lay-filter="switchCanaryStatus" class="id-input"
                       lay-text="开|关">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="l_application_log_detail" class="layui-form-label"
                   style="margin-top: -7px;width: 250px;">系统运行详细日志启用开关：</label>
            <div class="layui-input-block">
                <input id="l_application_log_detail" type="checkbox" name="application.log.detail" lay-skin="switch"
                       lay-filter="switch_l_application_log_detail_Status" class="id-input"
                       lay-text="开|关">
            </div>
        </div>

        <div id="extralConfigDiv">

        </div>

        <div class="layui-form-item" style="margin-top:  100px;">
            <button class="layui-btn" lay-filter="addConfig" lay-submit=""><i
                    class="layui-icon">&#xe608;</i>添加
            </button>
        </div>


        <!--<div class="layui-form-item">-->
        <!--<label for="" class="layui-form-label">-->
        <!--</label>-->
        <!--<button class="layui-btn" lay-filter="edit" lay-submit="">-->
        <!--修改-->
        <!--</button>-->
        <!--</div>-->
    </form>
</div>

<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;


        form.on('switch(switchCanaryStatus)', function (data) {
            // layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
            //     offset: '6px'
            // });

            var obj = this;

            var editSuc = editConfig(obj, this.checked, true);
            if (editSuc) {
                if (this.checked) {
                    layer.tips('温馨提示：灰度过滤器开关已打开，所有灰度过滤器将启用', data.othis);
                } else {
                    layer.tips('温馨提示：灰度过滤器开关已关闭，所有灰度过滤器将停用', data.othis);
                }
            }

        });

        form.on('switch(switch_l_application_log_detail_Status)', function (data) {
            // layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
            //     offset: '6px'
            // });

            var obj = this;

            var editSuc = editConfig(obj, this.checked, true);
            if (editSuc) {
                if (this.checked) {
                    layer.tips('温馨提示：系统运行详细日志开关已打开，系统将打印详细运行日志', data.othis);
                } else {
                    layer.tips('温馨提示：系统运行详细日志开关已关闭，系统将关闭打印详细运行日志', data.othis);
                }
            }
            if (!editSuc) {
                window.location.reload();
            }

        });


        form.on('submit(addConfig)', function (data) {

            x_admin_show('添加全局配置', './gateway-config-add.html?env=' + env + "&gateway=" + gateway + "&zuulFilterId=__global_config_relation__");

            return false;
        });


    });
</script>
<script>var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>